class Solution {
public:
    int minDays(vector<int>& bloomDay, long long m, long long k) {
        if (m * k > bloomDay.size()) {
            return -1;
        }
        int left = *min_element(bloomDay.begin(), bloomDay.end());
        int right = *max_element(bloomDay.begin(), bloomDay.end());
        int mid = 0;
        int ans = 0;
        while (left <= right) {
            int shu = 0;
            int zhi = k;
            mid = left + (right - left) / 2;
            for (int i = 0; i < bloomDay.size(); i++) {
                if (bloomDay[i] <= mid) {
                    zhi--;
                    if (zhi == 0) {
                        shu++;
                        zhi = k;
                    }
                } else {
                    zhi = k;
                }
            }
            if (shu >= m) {
                right = mid - 1;
                ans = mid;

            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
};